#include "grid.h"
#include <cmath>


Grid::Grid(int rows, int cols) : rows_(rows), cols_(cols) {
obstacles_ = std::vector<std::vector<bool>>(rows, std::vector<bool>(cols, false));
}


void Grid::addObstacle(int row, int col) {
    if (row >= 0 && row < rows_ && col >= 0 && col < cols_) {
     obstacles_[row][col] = true;
    }   
}


bool Grid::isObstacle(int row, int col) const {
    return obstacles_[row][col];
}


std::vector<std::pair<int, int>> Grid::getNeighbors(const std::pair<int, int>& position) const {
    std::vector<std::pair<int, int>> neighbors;
    
    int directions[8][2] = { {0,1}, {1,0}, {0,-1}, {-1,0}, {1,1}, {1,-1}, {-1,1}, {-1,-1} };

    for (const auto& dir : directions) {
        int new_row = position.first + dir[0];
	int new_col = position.second + dir[1];
	if (new_row >= 0 && new_row < rows_ && new_col >= 0 && new_col < cols_ && !isObstacle(new_row, new_col)) {
	    neighbors.emplace_back(new_row, new_col);
	}
    }
    return neighbors;
}


double Grid::cost(const std::pair<int, int>& from, const std::pair<int, int>& to) const {
     if (from.first != to.first && from.second != to.second) {
         return std::sqrt(2.0); 
     }
     return 1.0; 
}

int Grid::getRows() const {
     return rows_;
}

int Grid::getCols() const {
     return cols_;
}





